import gui.*;
import backend.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class NewServerFrame extends JFrame {
    protected Server server;
    protected JFrame new_server_frame;

    // Define sizes of elements for uniformity
    final static Dimension CONFIG_SIZE = new Dimension(350,200);

    public NewServerFrame() {
        super();
        this.new_server_frame = this;

        Dimension screen_size = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        Point loc = new Point((screen_size.width - CONFIG_SIZE.width)/2,
                              (screen_size.height - CONFIG_SIZE.height)/4);

        this.setTitle("New Server Configurator");
        this.setMinimumSize(new Dimension(500,200));
        this.setLocation(loc);

        JPanel config_panel = new JPanel();
        config_panel.setLayout(new BoxLayout(config_panel, BoxLayout.Y_AXIS));
        config_panel.setPreferredSize(CONFIG_SIZE);

        // Server name subpanel
        JPanel server_subpanel = new JPanel();
        server_subpanel.add(new JLabel("Server Name:"));
        final JTextField server_name = new JTextField(15);
        server_subpanel.add(server_name);
        config_panel.add(server_subpanel);

        // Username
        JPanel username_subpanel = new JPanel();
        username_subpanel.add(new JLabel("Username:"));
        final JTextField user_name = new JTextField(15);
        username_subpanel.add(user_name);
        config_panel.add(username_subpanel);

        // Remote dictionary path
        JPanel dict_subpanel = new JPanel();
        dict_subpanel.add(new JLabel("Dictionary file:"));
        final JTextField dict_name = new JTextField(30);
        dict_name.setText("/usr/local/share/freeradius/dictionary.rbac");
        dict_subpanel.add(dict_name);
        config_panel.add(dict_subpanel);
        
        // Remote user file path
        JPanel userfile_subpanel = new JPanel();
        userfile_subpanel.add(new JLabel("Users file:"));
        final JTextField user_file = new JTextField(30);
        user_file.setText("/usr/local/etc/raddb/users");
        userfile_subpanel.add(user_file);
        config_panel.add(userfile_subpanel);
        
        // Private key path -- TODO Change to FileChooser
        JPanel pk_file_subpanel = new JPanel();
        pk_file_subpanel.add(new JLabel("Public Key Path:"));
        final JTextField pk_path = new JTextField(15);
        pk_file_subpanel.add(pk_path);
        config_panel.add(pk_file_subpanel);

        JPanel button_subpanel = new JPanel();
        JButton create_button = new JButton("Create New Server");
        create_button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // Create server with above info
                    // XXX The user input is unchecked, so this is completely unsafe.
                    try {
                        Server server = new Server(server_name.getText());
                        server.setUser(user_name.getText());
                        server.setKeyFile(pk_path.getText());
                        server.setDictFile(dict_name.getText());
                        server.setUsersFile(user_file.getText());
                        // server.setUsersFile();// TODO Scheme to create this file
                        // server.setDictFile();// TODO Scheme to create this file
                        server.saveAppConfiguration();

                        // Create a new login frame and destroy this frame.  This is
                        // the easiest way to update the server list, etc.
                        JFrame new_login = new FReeconf();
                        new_server_frame.setVisible(false);
                        new_server_frame.dispose();
                    }
                    catch (Exception ex) {
                        JOptionPane.showMessageDialog(null,
                                                      "WHAT THE FUCK DID YOU DO!?" + ex.getMessage(),
                                                      "Error",
                                                      JOptionPane.ERROR_MESSAGE,
                                                      new ImageIcon("resources/cat.jpg"));
                    }
                }
            });
        button_subpanel.add(create_button);
        config_panel.add(button_subpanel);

        this.getContentPane().add(config_panel);
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
